package com.hh.test.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.Buffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.file.Paths;

public class FileMap {
	public static void main(String[] args) {

		long st = System.currentTimeMillis();

		String fileOut = "C:\\workspace\\w0\\test\\src\\test\\java\\com\\hh\\test\\io\\fileOut.txt";
		String in = "C:\\workspace\\w0\\test\\src\\test\\java\\com\\hh\\test\\io\\txt.txt";
		test2(fileOut, in);
		System.out.println(System.currentTimeMillis() - st);
	}

	static void test2(String fileOut, String in) {
		try (FileInputStream input = new FileInputStream(in); FileChannel channel = input.getChannel();) {
			MappedByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
			for (int i = 0; i < channel.size(); i++) {
				buffer.get();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	static void test1(String fileOut, String in) {
		try (FileChannel channel = FileChannel.open(Paths.get(in));
				FileOutputStream out = new FileOutputStream(fileOut);) {
			MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
			// byte[] buffers = new byte[(int) channel.size()];
			// buffer.get(buffers);
			//
			// out.write(buffers);
			for (int i = 0; i < channel.size(); i++) {
				buffer.get();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
